package com.chuangjiangx.member.stored.ddd.domain.event;

import com.chuangjiangx.domain.shared.model.PayEntry;
import com.chuangjiangx.domain.shared.model.PayTerminal;
import com.chuangjiangx.domain.shared.model.PayType;
import com.chuangjiangx.member.basic.ddd.domain.model.OperatorInfo;
import lombok.Getter;
import lombok.ToString;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author cj-ch
 * @date 2018/5/22 下午3:50
 */
@Getter
@ToString
public class MbrPaySuccessEvent {
    /**
     * ****会员信息*****
     */
    private Long memberId;
    private Long merchantId;


    /** *****支付信息***** */
    /**
     * order_pay表的订单编号
     */
    private String orderPayNumber;
    /**
     * 会员订单Id
     */
    private Long mbrOrderId;
    private BigDecimal amount;
    private BigDecimal realPayAmount;
    private PayEntry payEntry;
    private PayType payType;
    private Integer payChannelId;
    private PayTerminal payTerminal;
    /**
     * 会员系统的卡券核ID
     */
    private Long mbrHasCouponId;

    /**
     * ****操作人信息*****
     */
    private Long merchantUserId;
    private Long storeId;
    private Long storeUserId;
    private String storeAddress;
    private String merchantAddress;

    private Type serviceType;

    private MbrPaySuccessEvent() {
    }

    /**
     * 判断业务类型
     * 充值/消费
     */
    public Type serviceType() {
        if(this.serviceType != null){
            return serviceType;
        }
        if (Objects.equals(PayEntry.MSCARDPAY, this.payEntry)) {
            //支付入口是会员卡,则业务类型一定是消费
            return Type.CONSUMER;
        }
        return Type.CONSUMER;
    }

    public OperatorInfo getOperatorInfo(){
        return new OperatorInfo(
                this.merchantId,
                this.merchantUserId,
                this.storeUserId,
                this.storeId,
                this.storeAddress,
                this.merchantAddress
        );
    }



    public enum Type {
        /**
         * 充值业务
         */
        RECHARGE,
        /**
         * 消费业务
         */
        CONSUMER;
    }

    public static class MbrPaySuccessEventBuild {

        private Long memberId;
        private Long merchantId;


        private String orderPayNumber;
        private Long mbrOrderId;
        private BigDecimal amount;
        private BigDecimal realPayAmount;
        private PayEntry payEntry;
        private PayType payType;
        private Integer payChannelId;
        private PayTerminal payTerminal;
        private Long mbrHasCouponId;


        private Long merchantUserId;
        private Long storeId;
        private Long storeUserId;
        private String storeAddress;
        private String merchantAddress;

        private Type serviceType;

        public MbrPaySuccessEventBuild() {
        }

        public MbrPaySuccessEvent build() {
            MbrPaySuccessEvent e = new MbrPaySuccessEvent();
            e.memberId = this.memberId;
            e.merchantId = this.merchantId;
            e.orderPayNumber = this.orderPayNumber;
            e.mbrOrderId = this.mbrOrderId;
            e.amount = this.amount;
            e.realPayAmount = this.realPayAmount;
            e.payEntry = this.payEntry;
            e.payType = this.payType;
            e.payChannelId = this.payChannelId;
            e.payTerminal = this.payTerminal;
            e.mbrHasCouponId = this.mbrHasCouponId;
            e.merchantUserId = this.merchantUserId;
            e.storeId = this.storeId;
            e.storeUserId = this.storeUserId;
            e.storeAddress = this.storeAddress;
            e.merchantAddress = this.merchantAddress;
            e.serviceType = this.serviceType;
            return e;
        }

        public MbrPaySuccessEventBuild setMemberId(Long memberId) {
            this.memberId = memberId;
            return this;
        }

        public MbrPaySuccessEventBuild setMerchantId(Long merchantId) {
            this.merchantId = merchantId;
            return this;
        }

        public MbrPaySuccessEventBuild setOrderPayNumber(String orderPayNumber) {
            this.orderPayNumber = orderPayNumber;
            return this;
        }

        public MbrPaySuccessEventBuild setMbrOrderId(Long mbrOrderId) {
            this.mbrOrderId = mbrOrderId;
            return this;
        }

        public MbrPaySuccessEventBuild setAmount(BigDecimal amount) {
            this.amount = amount;
            return this;
        }

        public MbrPaySuccessEventBuild setRealPayAmount(BigDecimal realPayAmount) {
            this.realPayAmount = realPayAmount;
            return this;
        }

        public MbrPaySuccessEventBuild setPayEntry(PayEntry payEntry) {
            this.payEntry = payEntry;
            return this;
        }

        public MbrPaySuccessEventBuild setPayType(PayType payType) {
            this.payType = payType;
            return this;
        }

        public MbrPaySuccessEventBuild setPayChannelId(Integer payChannelId) {
            this.payChannelId = payChannelId;
            return this;
        }

        public MbrPaySuccessEventBuild setPayTerminal(PayTerminal payTerminal) {
            this.payTerminal = payTerminal;
            return this;
        }

        public MbrPaySuccessEventBuild setMbrHasCouponId(Long mbrHasCouponId) {
            this.mbrHasCouponId = mbrHasCouponId;
            return this;
        }

        public MbrPaySuccessEventBuild setMerchantUserId(Long merchantUserId) {
            this.merchantUserId = merchantUserId;
            return this;
        }

        public MbrPaySuccessEventBuild setStoreId(Long storeId) {
            this.storeId = storeId;
            return this;
        }

        public MbrPaySuccessEventBuild setStoreUserId(Long storeUserId) {
            this.storeUserId = storeUserId;
            return this;
        }

        public MbrPaySuccessEventBuild setStoreAddress(String storeAddress) {
            this.storeAddress = storeAddress;
            return this;
        }

        public MbrPaySuccessEventBuild setMerchantAddress(String merchantAddress) {
            this.merchantAddress = merchantAddress;
            return this;
        }

        public MbrPaySuccessEventBuild setServiceType(Type serviceType) {
            this.serviceType = serviceType;
            return this;
        }
    }


}
